import os
import time
import threading
def timer(mode):
    def wrapper(func):
        def deco(*args,**kw):
            type=kw.setdefault('type',None)
            start=time.time()
            func(*args,**kw)
            end=time.time()
            costTime=end-start
            print("{}-{}:Processing time is:{}".format(mode,type,costTime))
        return deco
    return wrapper

def cpu_io(a=1,b=1):
    count=1
    while(count<2000000):
        a+=a
        b+=b
        count+=count

def io_disk():
    with open("/home/gerrit/workspace/1.txt",'r+') as f:
        for x in range(20000):
            f.write("hello world\n")

@timer("[single_thread]")
def single_thread(func,type=""):
    for i in range(10):
        func()
@timer("[multi_thread]")
def multi_thread(func,type=""):
    thread_list=[]
    for i in range(10):
        t=threading.Thread(target=func,args=())
        thread_list.append(t)
        t.start()
    e=len(thread_list)
    while True:
        for th in thread_list:
            if not th.is_alive():
                e-=1
        if e<=0:
            break

def main():
    single_thread(cpu_io,type="cpu_io")
    multi_thread(cpu_io,type="cpu_io")
    single_thread(io_disk,type="io_disk")
    multi_thread(io_disk,type="io_disk")
if __name__=="__main__":
    main()
